<?php

namespace App\Store\Services\Log;

use Illuminate\Http\Request;
use App\Store\Models\Sysset\LogDoing as ModelsSyssetLogDoing;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Sysset\LogDoingSetting;
use Illuminate\Support\Facades\Redis;

class LogDoing
{
    
    public static function savelog(Request $request){

        $accountinfo= StoreCommon::getCurrentAccountinfo($request);

        $cp_id = isset($accountinfo['cp_id'])?$accountinfo['cp_id']:0;
        $store_ids=(isset($accountinfo['store_ids']) && is_numeric($accountinfo['store_ids']))?intval($accountinfo['store_ids']):0;
        $account_id=isset($accountinfo['id'])?$accountinfo['id']:0;
        $account_name=isset($accountinfo['account_name'])?$accountinfo['account_name']:'';
        $doing_title=self::getdoingtitle($request);
        if(!$doing_title){
            return ;
        }
        $data=[
            'cp_id'=>intval($cp_id),
            'store_ids'=>$store_ids,
            'account_id'=>$account_id,
            'account_name'=>$account_name,
            'client_ip'=>$request->ip(),
            'doing_at'=>time(),
            'doing_title'=>$doing_title,

        ];
        ModelsSyssetLogDoing::create($data);
    }


    private static function getdoingtitle(Request $request){
        $acturl=$request->decodedPath();
        $log=Redis::get('site_sysset_doing_setting');
        $log=$log?json_decode($log,true):[];
        if(is_array($log) && count($log)>0){
            if(isset($log[$acturl])){
                return $log[$acturl];
            }else{
                return '';
            }
        }else{
            $log=LogDoingSetting::pluck('doing_title','route')->toArray();
            Redis::set('site_sysset_doing_setting',json_encode($log));
            if(isset($log[$acturl])){
                return $log[$acturl];
            }else{
                return '';
            }
        }
    }
}